package com.hursing.tractor.baseview;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

/**
 * 父子关系解决绘制顺序和响应层次的问题，index解决搜索和引用的问题
 * @author liuhx
 *
 */
public class UIView {
	
	public static final int VIEW_ROOT = 0;
	public static final int VIEW_HANDY_CARD = 1;
	public static final int VIEW_START_BUTTON = 2;
	public static final int VIEW_FLIP_SPADE_BUTTON = 6;
	public static final int VIEW_FLIP_HEART_BUTTON = 5;
	public static final int VIEW_FLIP_CLUB_BUTTON = 4;
	public static final int VIEW_FLIP_DIAMOND_BUTTON = 3;
	public static final int VIEW_FLIP_JOKER_BUTTON = 7;
	public static final int VIEW_PAUSE_BUTTON = 8;
	public static final int VIEW_CONTINUE_BUTTON = 9;
	public static final int VIEW_BURY_BUTTON = 10;
	public static final int VIEW_TIP_BUTTON = 11;
	public static final int VIEW_PREVIOUS_BUTTON = 12;
	public static final int VIEW_PLAY_BUTTON = 13;
	public static final int VIEW_VIEW_KITTY_BUTTON = 14;
	public static final int VIEW_MASTER_LABEL = 15;
	public static final int VIEW_CURRENT_MASTER_LABEL = 16;
	public static final int VIEW_FLIPPER_LABEL = 17;
	public static final int VIEW_CURRENT_FLIPPER_LABEL = 18;
	public static final int VIEW_HOUSE_LABEL = 19;
	public static final int VIEW_CURRENT_HOUSE_LABEL = 20;
	public static final int VIEW_SCORE_LABEL = 21;
	public static final int VIEW_CURRENT_SCORE_LABEL = 22;
	public static final int VIEW_BOTTOM_CARD = 23;
	public static final int VIEW_RIGHT_CARD = 24;
	public static final int VIEW_TOP_CARD = 25;
	public static final int VIEW_LEFT_CARD = 26;
	public static final int VIEW_REMAINING_LABEL = 27;
	public static final int VIEW_CURRENT_REMAINING_LABEL = 28;
	public static final int VIEW_KITTY_CARD = 29;
	public static final int VIEW_STATUS_PANEL = 30;
	public static final int VIEW_DESKTOP_PANEL = 31;
	public static final int VIEW_FLIP_PANEL = 32;
	public static final int VIEW_PLAY_PANEL = 33;
	public static final int VIEW_ONE_BUTTON_PANEL = 34;
	public static final int VIEW_FLIPPED_SUIT = 35;
	public static int VIEW_COUNT = 36;		// view的总数，统计时用，不是真的index
	
	public static final UIView[] sViews = new UIView[VIEW_COUNT];

	protected Rect mFrame;
	protected int mTag;
	protected boolean mVisible;
	
	protected LinkedList<UIView> mSubviews;
	private UIView mSuperview;
	
	protected UIWindow mWindow;
	
	public UILabel toLabel() { return null; }
	public UITextButton toTextButton() { return null; }
	public UICardView toCardView() { return null; }
	public UIHandyCardView toHandyCardView() { return null; }
	public UIFlippedSuitView toFlippedSuitView() { return null; }
	
	// 组合关系
	public UIView addSubview(UIView view) {
		if (mSubviews == null) {
			mSubviews = new LinkedList<UIView>();
		}
		mSubviews.add(view);
		view.mSuperview = this;
		view.mWindow = mWindow;
		invalidate();
		return this;
	}
	
	public UIView addToView(UIView view) {
		view.addSubview(this);
		return this;
	}
	
	public void removeFromSuperview() {
		mSuperview.mSubviews.remove(this);
		mSuperview.invalidate();
		mWindow = null;
		mSuperview = null;
	}
	
	public UIView superview() {
		return mSuperview;
	}
	
	public List<UIView> subviews() {
		return mSubviews;
	}
	
	// 基本属性
	public UIView frame(Rect rect) {
		mFrame = rect;
		return this;
	}
	
	public final Rect frame() {
		return mFrame;
	}
	
	public final UIView tag(int tag) {
		mTag = tag;
		return this;
	}
	
	public final int tag() {
		return mTag;
	}
	
	// 是否在屏幕上可见，需其父view树都可见才是。iOS把visible命令为hidden，是否就是这样的原因？
	public final boolean onScreen() {
		if (mVisible == false || mWindow == null) {
			return false;
		} else if (this == mWindow) {
			return true;
		} else {
			return mSuperview.onScreen();
		}
	}
	
	public final UIView visible(boolean visible) {
		if (mVisible != visible) {
			mVisible = visible;
			if (mSuperview != null && mSuperview.onScreen()) {
				mWindow.invalidate(mFrame);
			}
		}
		return this;
	}
	
	public final boolean visible() {
		return mVisible;
	}
	
	// 子类做具体实现
	public UIView enabled(boolean enabled) { return this; }
	public boolean enabled() { return false; }
	
	public UIView paint(Paint paint) { return this; }
	public Paint paint() { return null; }
	
	public UIView text(String text) { return this; }
	public String text() { return null; }
	
	public UIView layout(int w, int h) { return this; }
	// 测量宽度，具有text的View可以在设定textSize后知道，Button在setFrame后根据height计算
	public int measureWidth() { return 0; }
	
	protected void doDraw (Canvas canvas) {}
	protected boolean doOnTouchDown(int x, int y) { return false; }
	
	public void onDraw (Canvas canvas) {
		if (mVisible) {
			doDraw(canvas);
			if (mSubviews != null) {
				ListIterator<UIView> itor = mSubviews.listIterator();
				while(itor.hasNext()) {
					itor.next().onDraw(canvas);
				}
			}
		}
	}
	
	public boolean onTouchDown(int x, int y) {
		if (mVisible && mFrame.contains(x, y)) {
			if (mSubviews != null) {
				ListIterator<UIView> itor = mSubviews.listIterator();
				while(itor.hasNext()) {
					if (itor.next().onTouchDown(x, y)) {
						return true;
					}
				}
			}
			return doOnTouchDown(x, y);
		}
		return false;
	}

	public void invalidate() {
		invalidate(mFrame);	
	}
	
	public void invalidate(Rect bounds) {
		if (onScreen()) {
			mWindow.invalidate(bounds);
		}
	}
}
